package com.rimi.service.impl;
import com.github.pagehelper.util.StringUtil;
import com.mysql.cj.result.Row;
import com.rimi.bean.CompanyInfoBean;
import com.rimi.bean.YqzcUser;
import com.rimi.commons.CommonResult;
import com.rimi.commons.ResultStatus;
import com.rimi.mapper.CompanyMapper;
import com.rimi.mapper.UserMapper;
import com.rimi.model.dto.ShippingAddressDto;
import com.rimi.model.vo.AddUserVo;
import com.rimi.service.CompanyService;
import com.rimi.service.UserService;
import com.rimi.util.PwdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author luc
 * @Date 2021/6/28
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {



    @Autowired
    UserMapper userMapper;
    @Autowired
    CompanyService companyService;

    @Override
    public YqzcUser findUserByUserPhoneNumber(String userPhoneNumber) {
        
        return userMapper.findUserByUserPhoneNumber(userPhoneNumber)  ;
    }

    @Override
    public YqzcUser findUserByEmail(String userEmail) {
        return userMapper.findUserByEmail(userEmail);
    }

    @Override
    public YqzcUser findUserByCompany(String infoCompanyName) {
        return userMapper.findUserByCompany(infoCompanyName);
    }

    @Override
    public YqzcUser findUserById(Integer id) {

        return userMapper.findUserById(id);
    }

    @Transactional
    @Override
    public CommonResult addUser(YqzcUser yqzcUser) {


        if (findUserByUserPhoneNumber(yqzcUser.getUserPhoneNumber())!=null){
           return CommonResult.onFailed(ResultStatus.PHONE_ADD_FAILED);
        }
        if (findUserByEmail(yqzcUser.getUserEmail())!=null ){
            return CommonResult.onFailed(ResultStatus.EMAIL_ADD_FAILED);
        }
        if (findUserByCompany(yqzcUser.getInfoCompanyName())!=null){
            return CommonResult.onFailed(ResultStatus.COMPANY_ADD_FAILED);
        }

        yqzcUser.setUserPassword(PwdUtil.encode(yqzcUser.getUserPassword()));

        int row = userMapper.addUser(yqzcUser);
        if (row==1){
            CompanyInfoBean companyInfoBean=new CompanyInfoBean();
            companyInfoBean.setInfoCompanyName(yqzcUser.getInfoCompanyName());
            companyInfoBean.setUserId(yqzcUser.getUserId());
            System.out.println(yqzcUser.getInfoCompanyName()+yqzcUser.getUserId());
            companyService.addCompany(companyInfoBean);
        }
        return row==1?CommonResult.onSuccess(null):CommonResult.onFailed(ResultStatus.ADD_FAILED);
    }

    @Override
    public CommonResult updateUserById(Integer id, String userPhoneNumber) {
        int row=userMapper.updateUserById(id,userPhoneNumber);
        return row==1?CommonResult.onSuccess(null):CommonResult.onFailed(ResultStatus.UPDATE_FAILED);
    }



    @Override
    public CommonResult addUerToSql(YqzcUser user) {
        if(StringUtils.hasText(user.getUserPassword())) {
            String encode = PwdUtil.encode(user.getUserPassword());
            user.setUserPassword(encode);
        }
        int row =  userMapper.addUser(user);
        return row==1?CommonResult.onSuccess(null):CommonResult.onFailed(ResultStatus.ADD_FAILED);
    }

    /**
     * 查询默认的收货地址
     * @param userId 用户ID
     * @return 地址
     */
    @Override
    public CommonResult<List<ShippingAddressDto>> findDefaultAddress(Integer userId) {
        List<ShippingAddressDto> shippingAddress =  userMapper.findDefaultAddress(userId);
        return shippingAddress.size()>0?CommonResult.onSuccess(shippingAddress):CommonResult.onFailed(ResultStatus.NOT_FIND_ADDRESS);
    }
    /**
     * 查询默认的收货地址信息
     * @param userId 用户ID
     * @return 地址
     */
    @Override
    public String findDefaultAddressInfo(Integer userId) {
        String addressInfo = userMapper.findDefaultAddressInfo(userId);
        return addressInfo != null ? addressInfo:"没有查询到收货地址";
    }
}
